<template>
  <div class="login">
    <h2>团队互评系统</h2>
    <div class="register-btn" @click="toRegister">注册</div>
    <div class="login-wrap">
      <el-form :label-position="labelPosition" label-width="80px" :model="formData">
          <el-form-item label="用户名">
            <el-input v-model="formData.account"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="formData.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loginHandler">登录</el-button>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import api from '../api/index'
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export default {
  setup () {
    const router = useRouter()
    const formData = reactive({
      account: '',
      password: ''
    })
    const loginHandler = async () => {
      const res = await api.login({ ...formData })
      if (res.data.code === 200) {
        localStorage.setItem('user_name', res.data.data.user_name)
        localStorage.setItem("user_id", res.data.data.user_id)
        router.push('/')
      } else {
        ElMessage.error(res.data.msg)
      }
    }
    function toRegister() {
      router.push('/register')
    }
    return {
      formData,
      loginHandler,
      toRegister
    }
  }
}
</script>

<style lang="scss" scoped>
  .login{
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    h2{
      position: fixed;
      left: 30px;
      top: 20px;
    }
    .register-btn{
      position: absolute;
      right: 50px;
      top: 20px;
      cursor: pointer;
      color: rgb(21, 86, 184);
    }
    .login-wrap{
      width: 400px;
      height: 250px;
      padding: 15px;
      border: 1px solid rgb(21, 86, 184);
      border-radius: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
</style>
